package com.lg.java.classload;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * 使用类加载器 加载类 并调用其方法
 */
public class Base {
    public static void main(String[] args) {
//        fun1();
        fun2();
    }

    public static void fun1() {
        MyClassLoader myClassLoader = new MyClassLoader("C:/coding/burning/study-java/target/classes/com/lg/java/classload/");
        try {
            Class<?> clazz = myClassLoader.findClass("TargetClass");
            Method method = clazz.getMethod("sayMess", String.class);
            // clazz.newInstance() = clazz.getConstructor().newInstance()
            method.invoke(clazz.newInstance(), "main invoke method");

            Constructor<?> constructor = clazz.getConstructor(String.class, Integer.class);
            constructor.newInstance("gg", 22);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fun2() {
        try {
            Class clz = Class.forName("com.lg.java.classload.TargetClass");
            Method method = clz.getMethod("sayMess", String.class);
            Constructor constructor = clz.getConstructor();
            Object object = constructor.newInstance();
            method.invoke(object, "gggg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}